
package br.ita.escolaita.view.converter;

import br.ita.escolaita.model.entity.Turma;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

/**
 *
 * @author matheusn
 */
public class TurmaConverter implements Converter{

    public Object getAsObject(FacesContext ctx, UIComponent comp, String value) {        
        UISelectItems selectItems = (UISelectItems) comp.getChildren().get(1);
        List listaSelectItem = (List) selectItems.getValue();
        for (Object o: listaSelectItem) {
            SelectItem  item = (SelectItem) o;
            Turma t = (Turma) item.getValue();
            if (t.getId().equals(Integer.valueOf(value))){
                return t;
            }
        }        
        return null;
    }

    public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {        
        if (obj == null){            
            return null;
        }
        if (obj instanceof String){            
            return "";
        }
        Turma t = (Turma)obj;        
        return t.getId().toString();
    }
}
